package online.longzipeng.cinemasys.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import online.longzipeng.cinemasys.common.annotion.NeedPermissions;
import online.longzipeng.cinemasys.common.exception.AppException;
import online.longzipeng.cinemasys.common.result.ResponseEnum;
import online.longzipeng.cinemasys.common.result.Result;
import online.longzipeng.cinemasys.entity.SysUserRole;
import online.longzipeng.cinemasys.service.SysUserRoleService;
import online.longzipeng.cinemasys.vo.sysuser.SysUserRoleSaveVO;
import online.longzipeng.cinemasys.vo.sysuser.SysUserRoleUpdateVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzp
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/sys-user-role")
@Api(value = "系统用户角色相关接口",description = "系统用户角色相关接口",tags = "系统用户角色相关接口")
public class SysUserRoleController {

    @Autowired
    SysUserRoleService sysUserRoleService;

    @PostMapping("/save")
    @NeedPermissions("sysUserRole:save")
    @ApiOperation("新增用户的角色")
    public Result save(@Valid @RequestBody SysUserRoleSaveVO sysUserRoleSaveVO){
        SysUserRole sysUserRole = new SysUserRole();
        BeanUtils.copyProperties(sysUserRoleSaveVO,sysUserRole);
        sysUserRoleService.add(sysUserRole);
        return Result.success(null,"用户的角色添加成功！！");
    }

    @GetMapping("/delete-one")
    @NeedPermissions("sysUserRole:delete")
    @ApiOperation("删除用户的角色")
    public Result deleteOne(@ApiParam(value = "用户的角色id",required = true)Long id){
        try {
            sysUserRoleService.removeById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"用户的角色删除成功！！");
    }

    @GetMapping("/delete")
    @NeedPermissions("sysUserRole:delete")
    @ApiOperation("删除多个用户的角色")
    public Result deleteOne(@ApiParam(value = "用户的角色id数组",required = true)Long[] ids){
        List<Long> idList = Arrays.stream(ids).collect(Collectors.toList());
        try {
            sysUserRoleService.removeByIds(idList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"用户的角色删除成功！！");
    }

    @PostMapping("/update")
    @NeedPermissions("sysUserRole:update")
    @ApiOperation("修改用户的角色")
    public Result update(@Valid @RequestBody SysUserRoleUpdateVO sysUserRoleUpdateVO) {
        SysUserRole sysUserRole = new SysUserRole();
        BeanUtils.copyProperties(sysUserRoleUpdateVO,sysUserRole);
        sysUserRoleService.updateSysUserRole(sysUserRole);
        return Result.success(null,"用户的角色修改成功！！");
    }
}
